<!DOCTYPE html>
<html class="x-admin-sm">

<head>
    <meta charset="UTF-8">
    <title>学校固定资产管理系统</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <link rel="stylesheet" href="./css/font.css">
    <link rel="stylesheet" href="./css/xadmin.css">
    <script src="./lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="./js/xadmin.js"></script>
    <!--[if lt IE 9]>
          <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
          <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
        <![endif]-->
</head>

<body>
    <div class="x-nav">
        <span class="layui-breadcrumb">
            <a>首页</a>
            <a>角色管理</a>
            <a>
                <cite>用户管理</cite>
            </a>
        </span>
        <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()" title="刷新">
            <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
        </a>
    </div>
    <div class="layui-fluid">
        <div class="layui-row layui-col-space15">
            <div class="layui-col-md12">
                <div class="layui-card">
                    <div class="layui-card-body ">
                        <form class="layui-form">
                            <div class="layui-inline">
                                <div class="layui-input-inline" style="width: 190px;">
                                    <input type="text" name="UserName" id="UserName " placeholder="用户名" autocomplete="off" class="layui-input">
                                </div>
                                <div class="layui-input-inline" style="width: 190px;">
                                    <input type="text" name="PhoneNum" id="PhoneNum" placeholder="手机号" autocomplete="off" class="layui-input">
                                </div>
                                <div class="layui-input-inline" style="width: 190px;">
                                    <select id="L_DepartmentId" name="DepartmentId" lay-filter="DepartmentId">
                                        <option value="">部门</option>
                                    </select>
                                </div>
                            </div>
                            <button class="layui-btn" id="searchBtn" lay-submit="" lay-filter="search">
                                <i class="layui-icon">&#xe615;查询</i>
                            </button>
                        </form>

                    </div>
                    <div class="layui-card-header">
                        <button class="layui-btn" onclick="xadmin.open('添加用户','./user-add.html',451,500)">
                            <i class="layui-icon"></i>添加</button>
                    </div>
                    <div class="layui-card-body ">
                        <table id="user"></table>
                    </div>

                </div>

            </div>
        </div>
    </div>
</body>

<script>
    //初始状态
    layui.use('table', function () {
        var table = layui.table;
        //表格实例化
        table.render({
            elem: '#user'
            , height: 500
            , headers: { 'token': sessionStorage.getItem("token") }
            , url: sessionStorage.getItem("api") + 'users/list'
            , where: { StatusStr: status }
            , page: true
            , request: {
                pageName: 'PageIndex'
                , limitName: 'PageSize'
            }
            , parseData: function (res) {
                return {
                    "code": res.Data.Code,
                    "msg": res.Data.Msg,
                    "count": res.Data.Count,
                    "data": res.Data.Data
                };
            }
            , cols: [
                [
                    { type: 'numbers', title: '序号', width: 100 }
                    , { field: 'UserName', title: '用户名' }
                    , { field: 'Email', title: '邮箱' }
                    , { field: 'PhoneNum', title: '手机号' }
                    , { field: 'DepartmentName', title: '所属部门' }
                    , { field: 'stopUser', title: '是否停用', templet: '#isStop' }, {
                        title: '操作',
                        templet: '#button'
                    }
                ]
            ]
        });




    });
</script>
<script type="text/html" id="button">
    <input type="button" class="layui-btn layui-btn-radius layui-btn-normal" lay-submit lay-filter = "edit" id="{{d.Id}}"  value="修改">
</script>
<script type="text/html" id="isStop">
    <input type = "checkbox" name = "stopUser"  lay-skin="switch" value="{{d.Id}}" lay-text = "启用|停用" lay-filter = "stopUser" {{ d.Stop? '': 'checked'}} >    
</script>

<script>
    var pid = "";
    layui.use(['form', 'layer', 'jquery'],
        function () {
            $ = layui.jquery;
            var form = layui.form;
            form.on('switch(stopUser)', function (data) {
                $.ajax({
                    type: "POST",
                    headers: { "token": sessionStorage.getItem("token") },
                    url: sessionStorage.getItem("api") + "users/" + data.value + "/ban_user",
                    dataType: "Json",
                    success: function (res) {
                        if (res.Code == 200) {
                            layer.msg(res.Data);
                        } else {
                            layer.msg(res.ErrorMessage);
                        }
                    }
                })

            });

            form.on('submit(edit)', function (data) {
                pid = data.elem.attributes.id.nodeValue
                layer.open({
                    type: 2,
                    area: ['451px', '500px'],
                    fix: false, //不固定
                    maxmin: true,
                    shadeClose: true,
                    shade: 0.4,
                    title: "编辑用户",
                    content: 'user-edit.html'
                });
            });


            //表格筛选
            layui.use('table', function () {
                var table = layui.table;
                //监听提交
                form.on('submit(search)',
                    function (data) {
                        table.reload('user', {
                            where: data.field
                            , page: {
                                curr: 1 //重新从第 1 页开始
                            }
                        });
                        return false;
                    });
            })

            //获取部门
            $.ajax({
                type: "GET",
                headers: { "token": sessionStorage.getItem("token") },
                url: sessionStorage.getItem("api") + "department/noadmininfos",
                dataType: "Json",
                success: function (res) {
                    if (res.Code == 200) {
                        var html = '';
                        for (var i = 0; i < res.Data.Count; i++) {
                                var item = res.Data.Data[i];
                            html += '<option value="' + item.Id + '">' + item.DepartmentName + '</option>'
                        }
                        $("#L_DepartmentId").append(html);
                        form.render();
                    } else {
                        layer.msg(res.ErrorMessage);
                    }
                }
            })


        });
</script>

</html>